<template>
	<view class="content">
		<!-- <view class="title">创建项目</view> -->
		<lonjin-form 
			@submit="submit" 
			:formData="formData" 
			@updateValue="updateValue" 
			@uploadPicture="uploadPicture"
			:config="config"
			@uploadMedia="uploadMedia"
			>
		</lonjin-form>
	</view>
</template>

<script>
	import {
		saveWxUserInfo
	} from '@/api/user.js'
	
	export default {
		data() {
			return {
				customerList:[],
				formData: {},
				config: [
					{
						label: '姓名',
						placeholder: '请输入',
						key: 'userName',
						type: 'input',
						// unit: '℃',
						inputType: 'text',
					},
					{
						label: '手机号',
						placeholder: '请输入',
						key: 'phone',
						type: 'input',
						// unit: '℃',
						inputType: 'idcard',
					},
					{
						label: '部门',
						placeholder: '请选择',
						key: 'deptName',
						type: 'picker',
						options: [
							{
								label: '管理员',
								value: '管理员'
							},
							{
								label: '项目管理',
								value: '项目管理'
							},
							{
								label: '拍摄',
								value: '拍摄'
							},
							{
								label: '后期',
								value: '后期'
							},
							{
								label: '财务',
								value: '财务'
							}
						],
					},
					// {
					// 	label: '头像',
					// 	placeholder: '请上传图片',
					// 	key: 'images',
					// 	type: 'uploadMedia',
					// },
					// {
					// 	label: '多图上传',
					// 	placeholder: '请上传图片',
					// 	key: 'picture',
					// 	type: 'pictureGroup',
					// 	max: 2,
					// }
				]
			}
		},
		onLoad() {
			this.getCustomerList()
		},
		methods: {
			updateValue(val) {
				this.formData = val
			},
			
			async getCustomerList() {
				uni.showLoading({
					title: '请稍等...'
				})
				let that = this
				var res = null;
				try {
					res = await listCustomer({
						data: {}
					});
					console.log(res.data)
					this.customerList = res.data
					
					let optionList = []
					this.customerList.forEach(item=>{
						optionList.push({label:item.company+'-'+item.customerName,value: item.customerId})
					});
					
					let newValue = {
						label: '对接人',
						placeholder: '请选择',
						key: 'customerId',
						type: 'picker',
						options: optionList,
					}
					
					this.$set(this.config, 5, newValue)

					console.log(this.config)
					
				} catch (err) {
					console.log(err)
				} finally{
					uni.hideLoading()
				}
			},
			
			// 提交数据
			submit(val) {
				console.log(val)
				if(val.deptName == '管理员'){
					val.roleType = 'SYSTEM_ADMIN'
				}else if(val.deptName == '项目管理'){
					val.roleType = 'PROJECT'
				}else if(val.deptName == '财务'){
					val.roleType = 'FINANCIAL'
				}else{
					val.roleType = 'GENERAL'
				}
				
				const res = saveWxUserInfo({
					params:val
				})
				if(res.code == 200){
					uni.navigateBack()
				}
				
			},
			
			// 图片/视频上传
			uploadMedia(e) {
				// 如果 在formData中没有定义该字段，则需要进行深拷贝
				const data = JSON.parse(JSON.stringify(this.formData))
				data[e.key] = e.tempFiles[0].tempFilePath
				this.formData = JSON.parse(JSON.stringify(data))
				
				/* 如果定义了formData: {images: ''}，则不需要进行深拷贝 
				this.formData[e.key] = e.tempFiles[0].tempFilePath
				*/
			},
			
			// 多图上传
			uploadPicture(e) {
				// 如果 在formData中没有定义该字段，则需要进行深拷贝
				const data = JSON.parse(JSON.stringify(this.formData))
				if(data[e.key] && data[e.key].length){
					data[e.key] = [...data[e.key], e.tempFiles[0].tempFilePath]
				}else{
					data[e.key] = [e.tempFiles[0].tempFilePath]
				}
				this.formData = JSON.parse(JSON.stringify(data))
				/* 如果定义了formData: {images: ''}，则不需要进行深拷贝 
				this.formData[e.key] = [...this.formData[e.key], ...e.tempFiles[0].tempFilePath]
				*/
			},
		}
	}
</script>

<style>
	.title{
		width: 100%;
		font-size: 30rpx;
		margin-bottom: 32rpx;
		font-weight: bold;
		padding: 32rpx 32rpx 0 32rpx;
	}
	.content {
		width: 100%;
	}
</style>
